package com.sit.library.service.impl;

import com.sit.library.entity.bean.Account;
import com.sit.library.entity.bean.Book;
import com.sit.library.entity.dao.AccountMapper;
import com.sit.library.entity.dao.BookMapper;
import com.sit.library.entity.vo.response.AccountVO;
import com.sit.library.service.AdminService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class AdminServiceImpl implements AdminService {

    @Resource
    private AccountMapper accountMapper;

    @Resource
    private BookMapper bookMapper;

    @Resource
    private PasswordEncoder passwordEncoder;

    @Override
    public List<AccountVO> getAccounts() {
        List<Account> accounts = accountMapper.findAll();
        List<AccountVO> vos = new ArrayList<>();
        for (Account account : accounts) {
            AccountVO vo = new AccountVO();
            vo.setId(account.getId());
            vo.setName(account.getName());
            vo.setContact(account.getContact());
            vo.setRole(account.getRole());
            vos.add(vo);
        }
        return vos;
    }

    @Override
    public String addAccount(Account account) {
        if (accountMapper.findByUsername(account.getName()) != null) {
            return "用户名已存在";
        }
        String encode = passwordEncoder.encode(account.getPassword());
        account.setPassword(encode);
        if (accountMapper.addAccount(account) != 0) {
            return "添加用户成功";
        } else {
            return "添加用户失败";
        }
    }

    @Override
    public String updateAccount(Account account) {
        Account oldAccount = accountMapper.findByUsername(account.getName());
        if (oldAccount != null && !Objects.equals(oldAccount.getId(), account.getId())) {
            return "用户名已存在";
        }
        String encode = passwordEncoder.encode(account.getPassword());
        account.setPassword(encode);
        if (accountMapper.updateAccount(account) != 0) {
            return "更新用户成功";
        } else {
            return "更新用户失败";
        }
    }

    @Override
    public String deleteAccount(int id) {
        if (accountMapper.deleteAccount(id) != 0) {
            return "删除用户成功";
        } else {
            return "删除用户失败";
        }
    }

    @Override
    public List<Book> getBooks() {
        return bookMapper.findAll();
    }

    @Override
    public String addBook(Book book) {
        if (bookMapper.addBook(book) != 0) {
            return "添加图书成功";
        } else {
            return "添加图书失败";
        }
    }

    @Override
    public String updateBook(Book book) {
        if (bookMapper.updateBook(book) != 0) {
            return "更新图书成功";
        } else {
            return "更新图书失败";
        }
    }

    @Override
    public String deleteBook(int id) {
        if (bookMapper.deleteBook(id) != 0) {
            return "删除图书成功";
        } else {
            return "删除图书失败";
        }
    }
}
